<?php
/**
 * @copyright	Copyright (C) 2008 Bideew. All rights reserved.
 * @license		GNU/GPL
* This file is part of Bideew.

* Bideew is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.

* Bideew is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with Bideew; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

	class Group {
		public $id;
		public $name;
		public $droits;

		public function __construct($id,$name) {
			$this->id = $id;
			$this->name = $name;
			if(is_numeric($id)){
				$this->getDroits($id);
			}
		}

		public function Add () {
			$query = "INSERT INTO groups (g_title) VALUES"
					."('".$this->g_title."');";
			//echo $query;
			return DBexec($query);
		}

		public function Update () {
			$query = "UPDATE groups "
					."set name = '".$this->name."' "
					."where id = ".$this->id;
			//echo "<br />".$query."<br />";
			return DBexec($query);
		}

		public function getDroits($id){
			$query = "SELECT m_id "
					."FROM droits_module "
					."WHERE g_id = ".$id;
			//echo "<br />".$query."<br />";
			$QueryRes = DBselect ($query);
			$this->droits = array();
			foreach ($QueryRes as $res) {
				$this->droits[$res['m_id']] = true;
			}
		}

		public function Delete () {
			$query = "Delete from groups where g_id = ".$this->g_id;
			//echo "<br>$query<br>";
			return DBexec($query);
		}

		public static function get($WhereClause = null, $OrderbyClause = null, $Array = null, $List = null) {
			//On veut tout ou juste les username
			if ($List) {$Query = "Select id,name from groups";}
			Else {$Query = "Select * from groups";}
			//restrictions ?
			if ($WhereClause != null) {$Query .= " where $WhereClause";}
			if ($OrderbyClause != null) {$Query .= " Order by $OrderbyClause";}
			//echo $Query;
			//Query Execution
			$QueryRes = DBselect ($Query);
			//Query Result Traitment
			if (count($QueryRes)!=0) {
				foreach ($QueryRes as $res) {
					if ($List != null ) {$groups[$res['id']] = $res['name'];}
					else {
						$groups[] = new Group($res['id'],$res['name']);
					}
				}
				//Result returns
				if (count($groups) == 1 && $WhereClause && !$Array && !$List) {$groups = $groups[0];}
				return $groups;
			}
		}
	}
?>